<?php
/*
+--------------------------------------------------------------------------
|   IKPHP [#RELEASE_VERSION#]
|   ========================================
|   By IKPHP Software
|   © 2013 - 2050 WeCenter. All Rights Reserved
|   http://www.ikphp.com
|   ========================================
|   Author: 小麦 <ikphp@sina.cn>
+---------------------------------------------------------------------------
*/
defined ( 'IN_IKPHP' ) or exit ( 'WebSite Access Denied' );
class action_trace{
    protected $tracePageTabs =  array('BASE'=>'基本','FILE'=>'文件','INFO'=>'流程','ERR|NOTIC'=>'错误','SQL'=>'SQL','DEBUG'=>'调试');

    // 行为扩展的执行入口必须是run
    public function run(&$params){ 
        if(!IS_AJAX && !IS_CLI) {
        	if(C('SHOW_PAGE_TRACE')){
            	echo $this->showTrace();
        	}
        	echo $this->showDegInfo(); 
        }
    }
    private function showDegInfo(){
    	if(!C('SHOW_PAGE_TRACE')){
    		G('beginTime',$GLOBALS['_beginTime']);
    		G('viewEndTime');
    	}
    	$html = "<script>(function(){var debuginfo = document.getElementById('debuginfo');
    	if(debuginfo!=undefined){debuginfo.innerHTML = 'Processed in ".G('beginTime','viewEndTime')." second(s),  ".N('db_query')."  queries';}})();</script>";
    	return $html;
    }
     /**
     * 显示页面Trace信息
     * @access private
     */
    private function showTrace() {
    	// 系统默认显示信息
        $files  =  get_included_files();
        $info   =   array(); $file_size = 0;
        foreach ($files as $key=>$file){
        	$_fsize = number_format(filesize($file)/1024,2);
        	$file_size = $file_size+$_fsize;
            $info[] = $file.' ( '.$_fsize.' KB )';
        }
        $trace  =   array();
        $base   =   array(
            '请求信息'  =>  date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']).' '.$_SERVER['SERVER_PROTOCOL'].' '.$_SERVER['REQUEST_METHOD'].' : '.__SELF__,
            '运行时间'  =>  $this->showTime(),
			'吞吐效率'  =>  number_format(1/G('beginTime','viewEndTime'),2).' req/s',
            '内存开销'  =>  MEMORY_LIMIT_ON?number_format((memory_get_usage() - $GLOBALS['_startUseMems'])/(1024*1024),2).' M':'不支持',
            '查询信息'  =>  N('db_query').' queries '.N('db_write').' writes ',
            '文件加载'  =>  '已加载 '.count(get_included_files()).' 个文件 共'.number_format($file_size,2).' KB',
            '缓存信息'  =>  N('cache_read').' gets '.N('cache_write').' writes ',
            '配置加载'  =>  count(C()),
            '会话信息'  =>  'SESSION_ID='.session_id(),
            );
        $debug  =   trace(); 
        $tabs   =   C('TRACE_PAGE_TABS',null,$this->tracePageTabs);
            
        foreach ($tabs as $name=>$title){
            switch(strtoupper($name)) {
                case 'BASE':// 基本信息
                    $trace[$title]  =   $base;
                    break;
                case 'FILE': // 文件信息
                    $trace[$title]  =   $info;
                    break;
                default:// 调试信息
                    $name       =   strtoupper($name);
                    if(strpos($name,'|')) {// 多组信息
                        $array  =   explode('|',$name);
                        $result =   array();
                        foreach($array as $name){
                            $result   +=   isset($debug[$name])?$debug[$name]:array();
                        }
                        $trace[$title]  =   $result;
                    }else{
                        $trace[$title]  =   isset($debug[$name])?$debug[$name]:'';
                    }
            }
        } 
            
        if($save = C('PAGE_TRACE_SAVE')) { // 保存页面Trace日志
            if(is_array($save)) {// 选择选项卡保存
                $tabs   =   C('TRACE_PAGE_TABS',null,$this->tracePageTabs);
                $array  =   array();
                foreach ($save as $tab){
                    $array[] =   $tabs[$tab];
                }
            }
            $content    =   date('[ c ]').' '.get_client_ip().' '.$_SERVER['REQUEST_URI']."\r\n";
            foreach ($trace as $key=>$val){
                if(!isset($array) || in_array($key,$array)) {
                    $content    .=  '[ '.$key." ]\r\n";
                    if(is_array($val)) {
                        foreach ($val as $k=>$v){
                            $content .= (!is_numeric($k)?$k.':':'').print_r($v,true)."\r\n";
                        }
                    }else{
                        $content .= print_r($val,true)."\r\n";
                    }
                    $content .= "\r\n";
                }
            }
            error_log(str_replace('<br/>',"\r\n",$content), 3,LOG_PATH.'trace/'.date('y_m_d').'_trace.log');
        }     
         
        unset($files,$info,$base);
        // 调用Trace页面模板
        ob_start();
        include C('TMPL_TRACE_FILE')?C('TMPL_TRACE_FILE'):IKPHP_PATH.'tmpl/ik_trace.tmpl';
        return ob_get_clean();           
    }
    /**
     * 获取运行时间
     */
    private function showTime() {
        // 显示运行时间
        G('beginTime',$GLOBALS['_beginTime']);
        G('viewEndTime');
        // 显示详细运行时间
        return G('beginTime','viewEndTime').'s ( Load:'.G('beginTime','loadTime').'s Init:'.G('loadTime','initTime').'s Exec:'.G('initTime','viewStartTime').'s Template:'.G('viewStartTime','viewEndTime').'s )';
    }        
}